<template>
  <div class="info-dialog">
    <el-dialog
      :visible.sync="visible"
      :title="title"
      :modal-append-to-body="true"
      :append-to-body="true"
      :destroy-on-close="false"
      :modal="true"
      :close-on-click-modal="false"
      top="10vh"
      width="850px"
      @closed="closed"
    >
      <el-form ref="formDatas" :model="formData" :rules="rules">
        <el-form-item label-width="100px" label="中文" prop="cnName">
          <el-input v-model="formData.cnName" placeholder="请输入中文" autocomplete="off" />
        </el-form-item>
        <el-form-item label-width="100px" label="英文" prop="enName">
          <el-input v-model="formData.enName" placeholder="请输入英文" autocomplete="off" />
        </el-form-item>
        <el-form-item label-width="100px" label="城市编码" prop="cityCode">
          <el-input v-model="formData.cityCode" placeholder="请输入编码" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div style="text-align: right;">
        <el-button @click="closed" :loading="loading">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    saveScene
  } from '@/api/51world/scene.js'

  export default {
    name: 'InfoDialog',
    components: {
    },
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed:{

    },
    watch:{
      properties: {
        handler: function(value) {
          this.visible = value.visible
          if (value.data) {
            this.formData.id = value.data.id || ''
            this.formData.cnName = value.data.cnName || ''
            this.formData.enName = value.data.enName || ''
            this.formData.cityCode = value.data.cityCode || ''

          } else {
            this.formData.id = ''
            this.formData.cnName = ''
            this.formData.enName = ''
            this.formData.cityCode = ''
          }
          if (value.type) {
            this.type = value.type
            if (value.type === '0') {
              this.title = '新增'
            }
            if (value.type === '1') {
              this.title = '编辑'
            }
          }
        },
        immediate: true,
        deep: false
      }
    },
    data(){
      return {
        visible:false,
        title:'',
        loading:false,
        formData: {
          id: '',
          cnName: '',
          enName: '',
          cityCode: '',
        },
        rules: {
          cnName: [{
            required: true,
            message: '请输入场景名称',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '长度在 1 到 50 个字符',
            trigger: 'blur'
          }
          ]
        },
        tableData: [],
        page: {
          cur: 1,
          limit: 10,
          total: 0
        },
      }
    },
    created() {
    },
    methods:{
      submitForm: function() {
        this.$refs.formDatas.validate((valid) => {
          if (valid) {
            this.loading = true
            saveScene(this.formData).then(rsp => {
              this.$message({
                type: 'success',
                message:rsp.message
              })
              this.closed()
            }).catch(err => {
              console.log(err);
            })
            .finally(() => {
              this.loading = false
            })
          }
        })
      },
      closed: function() {
        this.$emit('closed', false)
      },
    }
  }

</script>

<style>
</style>
